//
//  TXRewardVideo.m
//  TestTX
//
//  Created by Akira on 2020/1/19.
//  Copyright © 2020 Akira. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TXRewardVideo.h"
#import <Unity/UnityInterface.h>
#import "UnityAppController.h"

@implementation CTXRewardVideo
static CTXRewardVideo* sharedInstance=nil;
+(CTXRewardVideo*)Instance{
    @synchronized (self.class) {
        if(sharedInstance==nil){
            sharedInstance=[[self.class alloc] init];
        }
        
        return sharedInstance;
    }
}

- (void)loadAd
{
    //[super viewDidLoad];
    self.rewardVideoAd = [[GDTRewardVideoAd alloc] initWithAppId:[self strAppID] placementId:[self strUtilID]];
    self.rewardVideoAd.delegate = self;
    self.rewardVideoAd.videoMuted = NO; // 设置激励视频是否静音
    self.isVideoLoaded = NO;
    [self.rewardVideoAd loadAd];
}

- (void)gdt_rewardVideoAdVideoDidLoad:(GDTRewardVideoAd *)rewardedVideoAd
{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdVideoDidLoad()");
    self.isVideoLoaded = YES;
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdVideoDidLoad", "");
}
// 确认激励视频广告有效性之后，在合适的时机展示广告
- (IBAction)showAd{
    if (!self.isVideoLoaded) {
         return;
    }
    if (!self.rewardVideoAd.isAdValid) {
        NSLog(@"TXRewardVideo::showAd(): 广告失效，重新拉取");
        self.isVideoLoaded = NO;
        [self loadAd];
        return;
    }
    [self.rewardVideoAd showAdFromRootViewController:[self adViewControll]];
}

/**
 广告数据加载成功回调

 @param rewardedVideoAd GDTRewardVideoAd 实例
 */
- (void)gdt_rewardVideoAdDidLoad:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdDidLoad()");
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdDidLoad", "");
}

/**
 视频播放页即将展示回调

 @param rewardedVideoAd GDTRewardVideoAd 实例
 */
- (void)gdt_rewardVideoAdWillVisible:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdWillVisible()");
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdWillVisible", "");
}

/**
 视频广告曝光回调

 @param rewardedVideoAd GDTRewardVideoAd 实例
 */
- (void)gdt_rewardVideoAdDidExposed:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdDidExposed()");
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdDidExposed", "");
}

/**
 视频播放页关闭回调

 @param rewardedVideoAd GDTRewardVideoAd 实例
 */
- (void)gdt_rewardVideoAdDidClose:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdDidClose()");
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdDidClose", "");
    //[self loadAd];
}

/**
 视频广告信息点击回调

 @param rewardedVideoAd GDTRewardVideoAd 实例
 */
- (void)gdt_rewardVideoAdDidClicked:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdDidClicked()");
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdDidClicked", "");
}

/**
 视频广告各种错误信息回调

 @param rewardedVideoAd GDTRewardVideoAd 实例
 @param error 具体错误信息
 */
- (void)gdt_rewardVideoAd:(GDTRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAd(): code:%ld, msg:%@",(long)[error code],[error description]);
    
    char szThem[1024]={};
    sprintf(szThem,"code:%ld, msg:%s", [error code], [[error description]UTF8String]);
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAd", szThem);
}

/**
 视频广告播放达到激励条件回调

 @param rewardedVideoAd GDTRewardVideoAd 实例
 */
- (void)gdt_rewardVideoAdDidRewardEffective:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdDidRewardEffective()");
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdDidRewardEffective", "");
}

/**
 视频广告视频播放完成

 @param rewardedVideoAd GDTRewardVideoAd 实例
 */
- (void)gdt_rewardVideoAdDidPlayFinish:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"TXRewardVideo::gdt_rewardVideoAdDidPlayFinish()");
    UnitySendMessage("GDTAgent", "gdt_rewardVideoAdDidPlayFinish", "");
}
@end
void _LoadTXRewardVideoAD(){
    printf("TXRewardVideo::_LoadTXRewardVideoAD()");
    [[CTXRewardVideo Instance]loadAd];
}

void _ShowTXRewardVideoAD(){
    printf("TXRewardVideo::_ShowTXRewardVideoAD()");
    [[CTXRewardVideo Instance]showAd];
}

void _IsHasRewardVideo(){
    printf("TXRewardVideo::_IsHasRewardVideo()");
    char * pRet= [CTXRewardVideo Instance].isVideoLoaded?"true":"false";
    
    UnitySendMessage("GDTAgent", "IsHasRewardVideo", pRet);
}

void _InitTXRewardVideo(const char* pszAppID, const char* pszUtilID){
    printf("TXRewardVideo::_InitTXRewardVideo()");
    [CTXRewardVideo Instance].strAppID=[NSString stringWithUTF8String:pszAppID];
    [CTXRewardVideo Instance].strUtilID=[NSString stringWithUTF8String:pszUtilID];
    
    [CTXRewardVideo Instance].adViewControll=_UnityAppController.rootViewController;
}
